# 2024/12/28
# file name: data_explore.py

def show_top10(input_file):
    import pandas as pd

    input_file = '../data/GoodsOrder.csv'
    data = pd.read_csv(input_file)
    group = data.groupby(['Goods']).count().reset_index()
    sorted_group = group.sort_values('id', ascending=False)
    print(f'销量排行前十的商品销量：{sorted_group.head(10)}')

    import matplotlib.pyplot as plt

    x = sorted_group[:10]['Goods']
    y = sorted_group[:10]['id']
    plt.figure(figsize=(8, 4))
    plt.barh(x, y)
    plt.rcParams['font.sans-serif'] = 'SimHei'
    plt.xlabel('销量')
    plt.ylabel('商品类别')
    plt.title('销量排行前十的商品销量')
    plt.savefig('../tmp/top10.png')
    plt.show()

    data_nums = data.shape[0]
    for index, row in sorted_group[:10].iterrows():
        print(f'{row["Goods"]}的销量占比为{row["id"] / data_nums:.2%}')


def show_top10_types(input_file):
    import pandas as pd
    input_file1 = '../data/GoodsOrder.csv'
    input_file2 = '../data/GoodsTypes.csv'
    data = pd.read_csv(input_file1)
    types = pd.read_csv(input_file2)

    group = data.groupby(['Goods']).count().reset_index()
    sorted_group = group.sort_values('id', ascending=False).reset_index()
    data_nums = data.shape[0]
    del sorted_group['index']

    sort_links = pd.merge(sorted_group, types, on='Goods')
    sort_link = sort_links.groupby(['Types']).sum().reset_index()
    sort_link = sort_link.sort_values('id', ascending=False).reset_index()
    del sort_link['index']

    sort_link['count'] = sort_link.apply(lambda x: x['id'] / data_nums, axis=1)
    sort_link.rename(columns={'count': 'percent'}, inplace=True)
    print(f'各类别商品的销量及其占比：\n{sort_link}')
    out_file1 = '../tmp/percent.csv'
    sort_link.to_csv(out_file1, index=False, header=True, encoding='utf-8')

    import matplotlib.pyplot as plt

    data = sort_link['percent']
    labels = sort_link['Types']
    plt.figure(figsize=(8, 6))
    plt.pie(data, labels=labels, autopct='%1.2f%%', startangle=90)
    plt.rcParams['font.sans-serif'] = 'SimHei'
    plt.title('各类别商品的销量及其占比')
    plt.savefig('../tmp/percent.png')
    plt.show()


"""
+----------------+----------------+
|    Filename    | Attribute Name |
+----------------+----------------+
| GoodsOrder.csv |       id       |
|                |      Goods     |
+----------------+----------------+
| GoodsTypes.csv |      Goods     |
|                |      Types     |
+----------------+----------------+
"""

if __name__ == '__main__':
    # get_top10('../data/GoodsOrder.csv')
    # get_top10_types('../data/GoodsOrder.csv')

    import pandas as pd

    input_file1 = '../data/GoodsOrder.csv'
    input_file2 = '../data/GoodsTypes.csv'
    data = pd.read_csv(input_file1)
    types = pd.read_csv(input_file2)

    group = data.groupby(['Goods']).count().reset_index()
    sorted_group = group.sort_values('id', ascending=False).reset_index()
    data_nums = data.shape[0]
    del sorted_group['index']

    sort_links = pd.merge(sorted_group, types, on='Goods')
    selected = sort_links.loc[sort_links['Types'] == '非酒精饮料']
    child_num = selected['id'].sum()
    selected['child_percent'] = selected.apply(lambda x: x['id'] / child_num, axis=1)
    selected.rename(columns={'id': 'count'}, inplace=True)
    print(f'非酒精饮料的销量及其占比：\n{selected}')
    out_file1 = '../tmp/child_percent.csv'
    selected.to_csv(out_file1, index=False, header=True, encoding='utf-8')

    import matplotlib.pyplot as plt
    data = selected['child_percent']
    labels = selected['Goods']
    plt.figure(figsize=(8, 6))
    explode = [0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.08, 0.3, 0.1, 0.3]
    plt.pie(data, labels=labels, autopct='%1.2f%%', startangle=90, explode=explode, pctdistance=1.1, labeldistance=1.2)
    plt.rcParams['font.sans-serif'] = 'SimHei'
    plt.title('非酒精饮料的销量及其占比')
    plt.axis('equal')
    plt.savefig('../tmp/child_percent.png')
    plt.show()
